package com.hy.mydesktop.client.mvc.panel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.samples.resources.client.TestData;
import com.extjs.gxt.samples.resources.client.model.Stock;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.test.client.component.event.AppEvents;
import com.test.client.component.factory.GxtComponentFactory;
import com.test.client.component.factory.GxtDesktopComponentFactory;
import com.test.client.component.meta.GxtComponentMetaModel;
import com.test.client.component.meta.GxtComponentMetaNodeModel;
import com.test.client.component.type.ComponentTypeEnum;
import com.test.client.mvc.base.BaseController;
import com.test.client.mvc.desktop.DesktopView;

public class FormPanelView extends View{

	private Desktop desktop = null;
	private FormPanel formPanel = null;
	
	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel formPanel) {
		this.formPanel = formPanel;
	}

	public FormPanelView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		
		if (desktop==null) {
			//desktop = (Desktop)map.get("desktop");
			desktop = Registry.get("desktop");
		}		
		//读取事件中的所有数据信息
		 Map<String, Object> map = event.getData();
		
		//0007
		if (AppEvents.DESKTOP_APPLICATION_INIT==event.getType()) {
			
			String viewDataCode=((BaseController)this.getController()).getViewData();
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel = null;
			if (viewDataCode!=null) {
				/**
				 * 调用后台业务逻辑，获得viewDataCode，对应的元模型
				 */
				System.out.println("viewDataCode代码是："+viewDataCode+"执行getGxtComponentMetaNodeModel ()");
				if (viewDataCode.equals("menuBar_viewData_init_01")) {
					//gxtComponentMetaNodeModel = this.getGxtComponentMetaNodeModel();
				}
				
				
				
			}
			//gxtComponentMetaNodeModel = this.getGxtComponentMetaNodeModel();
			gxtComponentMetaNodeModel = ((BaseController)(this.getController())).getGxtComponentMetaNodeModel();
	
			/**
			 * 三个参数
			 * 两个参数，一个返回值
			 */
			GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel();
			List<GxtComponentMetaNodeModel> list=new LinkedList<GxtComponentMetaNodeModel>();
			list.add(gxtComponentMetaNodeModel);
			gxtComponentMetaModel.setList(list);
			
			formPanel = GxtComponentFactory.createGxtComponent(gxtComponentMetaModel);
		   
			BaseController parentController =(BaseController)(((BaseController)this.getController()).getParent());
			Map<BaseController, Component> controllerandcomponent = (Map<BaseController, Component>)map.get("controllerandcomponent");
			LayoutContainer layoutContainer = (LayoutContainer) controllerandcomponent.get(parentController);
		    if (layoutContainer!=null) {
		    	String layoutData = formPanel.getData("layoutdatainparentcontainer");
				if (layoutData!=null) {
					if (layoutContainer.getLayout() instanceof ColumnLayout) {
						layoutContainer.add(formPanel,new ColumnData(Double.parseDouble(layoutData)));
					}else if (layoutContainer.getLayout() instanceof FormLayout) {
						layoutContainer.add(formPanel,new FormData(layoutData));
					}else {
						//TODO 可以以后补充布局
						//layoutContainer.add(contentPanel,new ColumnData(Integer.parseInt(layoutData)));
						layoutContainer.add(formPanel);
					}
				}else {
					if (layoutContainer.getLayout() instanceof ColumnLayout) {
						layoutContainer.add(formPanel,new ColumnData(.5));
					}else if (layoutContainer.getLayout() instanceof FormLayout) {
						layoutContainer.add(formPanel,new FormData("100%"));
					}else {
				    	layoutContainer.add(formPanel);
					}

				}
		    	//layoutContainer.add(formPanel);
			}
		    System.err.println("formPanel 的parent"+layoutContainer);
		    
		};
		
		if (AppEvents.FORM_BUTTON_SUBMINT_ONCLICK==event.getType()) {
			
			/*Grid<ModelData> grid =null;
			for(Iterator<Component> it = gridPanel.iterator(); it.hasNext(); ) {
				
				LayoutContainer layoutContainer = (LayoutContainer)it.next();
				for(Iterator<Component> subIt = layoutContainer.iterator(); subIt.hasNext(); ) {
					
					Component subComponent = subIt.next();
					System.err.println("-----------gridPanel-------Child-----------"+subComponent.getClass());
					if (subComponent instanceof Grid) {
						grid = (Grid)subComponent;
					}
					
				} 								
			} */
			
			
			Shortcut s111 = new Shortcut();
			s111.setText("Grid 22222");
			s111.setId("grid-win-22222222");
		   
		    //添加一个快捷方式
			desktop = (Desktop)Registry.get(DesktopView.DESKTOP);
			if (desktop==null) {
				desktop = new Desktop();
			}
			System.out.println(desktop);
		    desktop.addShortcut(s111);
		};
		//根据事件类型和事件中包含的数据，响应事件
		if(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT==event.getType()){//根据事件类型，处理事件
			//根据事件中包含的数据，响应事件，即创建并初始化窗口
			
		//	this.createDynamicDesktop2(event);//最终创建出了桌面快捷方式（View视图）
			/**
			 * 何时添加控件的事件
			 * 1：创建控件的过程中，添加
			 * 识别GxtComponentMetaNodeModel中，Map中某个指，来设置它是否有AppEvent事件，
			 * 如何有AppEvent事件，需要被监听。
			 * 需要在Shortcut工厂中，通过ShortcutModelEnum.AppEven来判断，是否有事件需要监听
			 * 如果判断有，
			 * 根据Shortcut特定的方式，添加事件监听。如 Shortcut.addListener()
			 * 
			 * 2：控件创建完成后，添加
			 * 控件创建完成后，返回的是一个Desktop。
			 * 如何知道，改为哪个控件添加事件
			 */
			/**
			 * 为每个快捷方式，注册单击事件（包括业务事件）
			 */
			//List<Shortcut> list = desktop.getShortcuts();
			//list.get(0).
		}
	}
	
	private GxtComponentMetaModel getDesktopMetaModel() {
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("component_model_type", ComponentTypeEnum.DESKTOP_MODEL);
		GxtComponentMetaNodeModel node1=new GxtComponentMetaNodeModel(map1, 1, 6);
		
		Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("component_model_type", ComponentTypeEnum.SHORTCUT_MODEL);
		//TODO map2.put("appEventType", AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
		map2.put("appEventType", AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
		GxtComponentMetaNodeModel node2=new GxtComponentMetaNodeModel(map2, 2, 3);
		
		Map<String, Object> map3=new HashMap<String, Object>();
		map3.put("component_model_type", ComponentTypeEnum.SHORTCUT_MODEL);
		GxtComponentMetaNodeModel node3=new GxtComponentMetaNodeModel(map3, 4, 5);
		
		List<GxtComponentMetaNodeModel> list = new ArrayList<GxtComponentMetaNodeModel>();
		list.add(node1);
		list.add(node2);
		list.add(node3);
		GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel();
		gxtComponentMetaModel.setList(list);
		
		return gxtComponentMetaModel;
		
	}
	
	
	 /**
	 * 模拟从后台传过来的关于Window控件的元模型信息
	 */
	private GxtComponentMetaNodeModel getGxtComponentMetaNodeModel2() {
		/*Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("component_model_type", ComponentTypeEnum.FORMPANEL_MODEL);
			
		//map2.put("title", "11111111111createWindow-----LoginView----AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER");	
		GxtComponentMetaNodeModel node2=new GxtComponentMetaNodeModel(map2);
		return node2;*/
		
		//创建提交和取消按钮
		Map<String, Object> btnSubmit=new HashMap<String, Object>();
		btnSubmit.put("component_model_type", ComponentTypeEnum.BUTTON_MODEL);
		btnSubmit.put("text", "sumFormPanel确定");
		GxtComponentMetaNodeModel btnSubmit1=new GxtComponentMetaNodeModel(btnSubmit);
		
		Map<String, Object> btnquxiao=new HashMap<String, Object>();
		btnquxiao.put("component_model_type", ComponentTypeEnum.BUTTON_MODEL);
		btnquxiao.put("text", "取消");
		GxtComponentMetaNodeModel btn1=new GxtComponentMetaNodeModel(btnquxiao);
		
		List<GxtComponentMetaNodeModel> list1=new ArrayList<GxtComponentMetaNodeModel>();
		list1.add(btnSubmit1);
		list1.add(btn1);
		
		
		
		Map<String, Object> MaxPanel=new HashMap<String, Object>();
//		MaxPanel.put("component_model_type", ComponentTypeEnum.FORMPANEL_MODEL);
		MaxPanel.put("component_model_type_index", ComponentTypeEnum.FORMPANEL_MODEL.ordinal());
		MaxPanel.put("autowidth", true);
		MaxPanel.put("autoheight", true);
		MaxPanel.put("frame", true);
		MaxPanel.put("headervisible", false);
		MaxPanel.put("addbutton", list1);
		MaxPanel.put("buttonalign", "CENTER");
		GxtComponentMetaNodeModel node5=new GxtComponentMetaNodeModel(MaxPanel);
		
		return node5;
	}
	
	 //TODO 数据加载遍历
	/*public boolean canHandle(AppEvent event) {
	    if (supportedEvents != null && supportedEvents.contains(event.getType())) return true;
	    if (children != null) {
	      for (Controller c : children) {
	        if (c.canHandle(event)) return true;
	      }
	    }
	    return false;
	  }*/
	
}
